package com.tgy.algorithm.base01.review;

/**
 *  [1,1,1,2,4]
 *
 */
public class _数组最大和 {


    public static int positiveIntegerArr(int[] nums, int k) {

        if (nums == null || nums.length == 0) {
            return -1;
        }

        int len = nums.length;

        int left = 0;
        int right = 0;
        int sum = 0;
        int retLen = -1;
        while (left < len) {
            while (right < len && sum + nums[right] <= k) {
                sum += nums[right++];
            }

            if (sum == k) {
                retLen = Math.max(retLen,right - left);
            }

            sum -= nums[left++];
        }

        return retLen;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1,1,1,2,4};
        int ret = positiveIntegerArr(nums, 5);
        System.out.println(ret);
    }
}
